package com.huawei.secure.android.common.encrypt.keystore.rsa;

import android.annotation.TargetApi;
import android.os.Build.VERSION;
import android.security.keystore.KeyGenParameterSpec.Builder;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.spec.MGF1ParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource.PSpecified;

public abstract class RSAEncryptKS {
    private static final String A = "AndroidKeyStore";
    private static final String E = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
    private static final int F = 2048;
    private static final String TAG = "RSAEncryptKS";
    private static final String i = "";

    public static String encrypt(String str, String str2) {
        Exception e;
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            Log.e(TAG, "alias or content is null");
            return "";
        } else if (b()) {
            try {
                Key g = g(str);
                if (g == null) {
                    Log.e(TAG, "Public key is null");
                    return "";
                }
                Cipher instance = Cipher.getInstance(E);
                instance.init(1, g, new OAEPParameterSpec(AaidIdConstant.SIGNATURE_SHA256, "MGF1", new MGF1ParameterSpec("SHA-1"), PSpecified.DEFAULT));
                return Base64.encodeToString(instance.doFinal(str2.getBytes("UTF-8")), 0);
            } catch (GeneralSecurityException e2) {
                e = e2;
                Log.e(TAG, "RSA encrypt exception : " + e.getMessage());
                return "";
            } catch (UnsupportedEncodingException e3) {
                e = e3;
                Log.e(TAG, "RSA encrypt exception : " + e.getMessage());
                return "";
            }
        } else {
            Log.e(TAG, "sdk version is too low");
            return "";
        }
    }

    public static String decrpyt(String str, String str2) {
        Exception e;
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            Log.e(TAG, "alias or encrypted content is null");
            return "";
        } else if (b()) {
            try {
                Key privateKey = getPrivateKey(str);
                if (privateKey == null) {
                    Log.e(TAG, "Private key is null");
                    return "";
                }
                Cipher instance = Cipher.getInstance(E);
                instance.init(2, privateKey, new OAEPParameterSpec(AaidIdConstant.SIGNATURE_SHA256, "MGF1", new MGF1ParameterSpec("SHA-1"), PSpecified.DEFAULT));
                return new String(instance.doFinal(Base64.decode(str2, 0)), "UTF-8");
            } catch (GeneralSecurityException e2) {
                e = e2;
                Log.e(TAG, "RSA decrypt exception : " + e.getMessage());
                return "";
            } catch (UnsupportedEncodingException e3) {
                e = e3;
                Log.e(TAG, "RSA decrypt exception : " + e.getMessage());
                return "";
            }
        } else {
            Log.e(TAG, "sdk version is too low");
            return "";
        }
    }

    @TargetApi(23)
    private static KeyPair f(String str) {
        KeyPair keyPair = null;
        if (i(str)) {
            Log.e(TAG, "Key pair exits");
        } else {
            try {
                Log.i(TAG, "generate key pair.");
                KeyPairGenerator instance = KeyPairGenerator.getInstance("RSA", A);
                instance.initialize(new Builder(str, 2).setDigests(new String[]{AaidIdConstant.SIGNATURE_SHA256, "SHA-512"}).setEncryptionPaddings(new String[]{"OAEPPadding"}).setKeySize(2048).build());
                keyPair = instance.generateKeyPair();
            } catch (GeneralSecurityException e) {
                Log.e(TAG, "generateKeyPair exception: " + e.getMessage());
            }
        }
        return keyPair;
    }

    private static PublicKey g(String str) {
        if (!i(str)) {
            f(str);
        }
        Certificate h = h(str);
        if (h != null) {
            return h.getPublicKey();
        }
        return null;
    }

    private static Certificate h(String str) {
        Exception e;
        try {
            KeyStore instance = KeyStore.getInstance(A);
            instance.load(null);
            return instance.getCertificate(str);
        } catch (GeneralSecurityException e2) {
            e = e2;
            Log.e(TAG, "load public key exception : " + e.getMessage());
            return null;
        } catch (IOException e3) {
            e = e3;
            Log.e(TAG, "load public key exception : " + e.getMessage());
            return null;
        }
    }

    private static PrivateKey getPrivateKey(String str) {
        Exception e;
        if (!i(str)) {
            return null;
        }
        PrivateKey privateKey;
        try {
            KeyStore instance = KeyStore.getInstance(A);
            instance.load(null);
            privateKey = (PrivateKey) instance.getKey(str, null);
        } catch (GeneralSecurityException e2) {
            e = e2;
            Log.e(TAG, "get private key exception : " + e.getMessage());
            privateKey = null;
            return privateKey;
        } catch (IOException e3) {
            e = e3;
            Log.e(TAG, "get private key exception : " + e.getMessage());
            privateKey = null;
            return privateKey;
        }
        return privateKey;
    }

    private static boolean b() {
        return VERSION.SDK_INT >= 23;
    }

    private static boolean i(String str) {
        Exception e;
        try {
            KeyStore instance = KeyStore.getInstance(A);
            instance.load(null);
            if (instance.getKey(str, null) != null) {
                return true;
            }
            return false;
        } catch (GeneralSecurityException e2) {
            e = e2;
            Log.e(TAG, "key pair exists exciption : " + e.getMessage());
            return false;
        } catch (IOException e3) {
            e = e3;
            Log.e(TAG, "key pair exists exciption : " + e.getMessage());
            return false;
        }
    }
}
